/*
 * Created on Jun 27, 2011
 *
 * Spectro-Edit is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Spectro-Edit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */
package org.naivecode.audio;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class TestFeatureSpace {
	@Test
	public void atestFindExtrema() {
		float[] data = new float[1000];
		float[] noisySin = new float[data.length];

		data[0] = 0.5f;
		// logistic function provides deterministic chaos
		for (int i = 1; i < data.length; i++) {
			data[i] = 3.5929f * data[i - 1] * (1 - data[i - 1]);
		}
		// chaos needs smoothing out
		final int widowHalf = 10;
		for (int i = widowHalf; i < data.length - widowHalf; i++) {
			float meanValue = 0;
			for (int j = -widowHalf; j < widowHalf; j++) {
				meanValue += data[i + j];
			}
			data[i] = meanValue / (widowHalf * 2);
			// modulate sine
			noisySin[i] = (float) (data[i] * Math.sin(((float) i / data.length)
					* Math.PI * 6));
		}
		FeatureSpace fe = new FeatureSpace(data.length);
		List<Extremum> exts = new ArrayList<Extremum>();
		fe.findExtremums(noisySin, exts, 0.01f);
		for (Extremum ex : exts) {
			System.out.println(ex.frequency);
		}
	}
	@Test
	public void testFindExtremaCutoff() {
		System.out.println("cutoff");
		float[] data = new float[1000];
		float[] noisySin = new float[data.length];

		data[0] = 0.5f;
		// logistic function provides deterministic chaos
		for (int i = 1; i < data.length; i++) {
			data[i] = 3.5929f * data[i - 1] * (1 - data[i - 1]);
		}
		// chaos needs smoothing out
		final int widowHalf = 10;
		for (int i = widowHalf; i < data.length - widowHalf; i++) {
			float meanValue = 0;
			for (int j = -widowHalf; j < widowHalf; j++) {
				meanValue += data[i + j];
			}
			data[i] = meanValue / (widowHalf * 2);
			// modulate cutoff sine
			final double sine = Math.min(0.7, Math.sin(((float) i / data.length)
					* Math.PI * 6));
			noisySin[i-widowHalf] = (float) (data[i] * sine);
			//System.out.println(noisySin[i]);
		}
		FeatureSpace fe = new FeatureSpace(data.length);
		List<Extremum> exts = new ArrayList<Extremum>();
		fe.findExtremums(noisySin, exts, 0.005f);
		fe.simplify(exts, 10);
		for (Extremum ex : exts) {
			System.out.println(ex);
		}
	}

}
